package leetcode.leetcode_515;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        while (!nodes.isEmpty()) {
            int size = nodes.size();
            int maxNumber = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode poll = nodes.poll();
                maxNumber = Math.max(maxNumber, poll.val);
                if (poll.left != null) {
                    nodes.add(poll.left);
                }
                if (poll.right != null) {
                    nodes.add(poll.right);
                }
            }
            list.add(maxNumber);
        }
        return list;
    }
}